package com.panghu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.panghu.entity.ApiResult;
import com.panghu.entity.Student;
import com.panghu.service.impl.StudentServiceImpl;
import com.panghu.utils.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 学生信息表 前端控制器
 * </p>
 *
 * @author huahua
 * @since 2022-03-19
 */
@RestController
public class StudentController {

    @Autowired
    private StudentServiceImpl studentService;

    @GetMapping("/students/{page}/{size}")
    public ApiResult findAll(@PathVariable Integer page, @PathVariable Integer size) {
        //分页查询所有学生
        Page<Student> utilPage = new Page<>(page,size);
        Page<Student> page1 = studentService.page(utilPage);
        return ApiResultHandler.success(page1);
    }

    @GetMapping("/student/{studentId}")
    public ApiResult findById(@PathVariable("studentId") Integer studentId) {
        //按id进行学生查询
        Student stu = studentService.getById(studentId);
        return ApiResultHandler.success(stu);
    }

    @DeleteMapping("/student/{studentId}")
    public ApiResult deleteById(@PathVariable("studentId") Integer studentId) {
        //按id进行学生删除
        boolean remove = studentService.removeById(studentId);
        return ApiResultHandler.success(remove);
    }

    @PutMapping("/studentPWD")
    public ApiResult updatePwd(@RequestBody Student student) {
        //更新学生的密码
        boolean res = studentService.saveOrUpdate(student);
        return ApiResultHandler.success(res);
    }
    @PutMapping("/student")
    public ApiResult update(@RequestBody Student student) {
        //更新学生信息
        boolean b = studentService.saveOrUpdate(student);
        return ApiResultHandler.success(b);
    }

    @PostMapping("/student")
    public ApiResult add(@RequestBody Student student) {
        //添加一条学生记录
        boolean save = studentService.save(student);
        return ApiResultHandler.success(save);
    }
}

